var sock;
var progressInterval;
window.onload = function()
{
	document.getElementById('form').addEventListener('submit', play , false);
	addButton('previous')
	addButton('next')
	addButton('stop')
	addButton('plus')
	addButton('min')
	sock = new ConnectionClass(true);
	sock.start(messageHandler);
	var pauseButton = document.getElementById('pause');
	pauseButton.addEventListener('click', function()
	{
		clickHandle('pause');
		clearInterval(progressInterval);
	}, false);
	interval()
}

function addButton(id, handler)
{
	document.getElementById(id).addEventListener('click', function(){clickHandle(id)}, false);
}

function interval()
{
	clearInterval(progressInterval);
	progressInterval = setInterval(clickHandle, 2000, '%');
}
function messageHandler(msg)
{
	if(msg.match(/^\d+$/))
	{
		document.getElementById('progress').value = msg;
	}
	else if(msg == 'None')
	{
		document.getElementById('progress').value = 0;
	}
	else
	{
		parts = msg.split('|_|')
		document.getElementById('log').innerHTML += "<div class=\"song\" onclick=\"clickHandle('fc " + parts[1] + "')\">" + parts[0] + "</div>";
	}
}

function clickHandle(msg)
{
	msg = (msg == 'plus') ? '+' : msg;
	msg = (msg == 'min') ? '-' : msg;
	sock.send(msg);
	return false;
}

function play()
{
	clickHandle('yt ' + document.getElementById('search').value);
	// document.getElementById('log').innerHTML += "<div class=\"song\">" + document.getElementById('search').value + "</div>";
	document.getElementById('search').value = '';
}